package com.qiang.manager.dao;

import com.qiang.manager.domain.Teacher;

import java.util.ArrayList;

public class TeacherDaoImp implements IteacherDao {

    private static ArrayList<Teacher> array = new ArrayList<>();

    static {
        Teacher tea1 = new Teacher("001", "张老师", 35, 5593.6);
        Teacher tea2 = new Teacher("002", "李老师", 32, 4593.6);
        array.add(tea1);
        array.add(tea2);
    }

    @Override
    public void addTeacher(Teacher teacher) {
        array.add(teacher);

    }

    @Override
    public void deleteTeacher(String id) {
        int index = getIndex(id);
        array.remove(index);

    }

    @Override
    public void updateTeacher(String id, Teacher newtea) {
        int index = getIndex(id);
        array.set(index,newtea);

    }

    @Override
    public ArrayList<Teacher> findTeacher() {
        return array;
    }

    //getindex方法
    public int getIndex(String id) {
        int index = -1;
        //遍历集合
        for (int i = 0; i < array.size(); i++) {

            //判断非空，并且是否跟输入的id相同
            if (array != null && (array.get(i).getId()).equals(id)){
                index = i;
                return index;
            }
        }
        return index;
    }
}
